<?php

// Put your device token here (without spaces):
// 从用户手机上传,和Android的Send_id是一个概念
//$deviceToken = 'cf0df79b515b9d6cb6e83f2e3d2576e3cf437d257c740c6b498b246339253aec';
$deviceToken = '3877b64bae5a96216bc6b9d9b23eba0837bf2ac2252a174a693bb2c54190039e';//sendid

// Put your private key's passphrase here:
// ck.pem文件的密码
$passphrase = 'maszy.cn';

// Put your alert message here:
$message = 'netcube is 123321123!';//content

////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();

// ck.pem这个文件类似于Android 的Application Server key,但是这里是一个ck文件，所以帮忙在cp里边，如果create iOS application的时候，添加一个上传ck.pem的入口和一个填写ck.pem密码的框，Application server Key的框在IOS application创建的时候可以去掉.
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
	'ssl://gateway.sandbox.push.apple.com:2195', $err,
	$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

if (!$fp)
	exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
	'alert' => $message,
	'sound' => 'default'
	);

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
	echo 'Message not delivered' . PHP_EOL;
else
	echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);
